package com.intel.factorytest.cfparser.concrete;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;

public class XmlConfParser extends AConfigFileParser{


    private XmlContentHandler xmlContentHandler;
    public XmlConfParser(Context context) {
        super(context);
        xmlContentHandler = new XmlContentHandler(context);
    }

    @Override
    protected List<Map<String, Object>> readConfigFile(InputStream in,
            int isSys, int isSysForPcba, String localeString, String sdPath) {
        try {
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser saxparser = spf.newSAXParser();
            saxparser.parse(in, xmlContentHandler);
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    class XmlContentHandler extends DefaultHandler {

        private ConfigItem mConfigItem;
        private String mTagName;
        private Editor mEditor;
        private Integer mType;
        public XmlContentHandler(Context context) {
            SharedPreferences share = context.getSharedPreferences("case_config", Context.MODE_PRIVATE);
            mEditor = share.edit();
            mEditor.clear();
        }

        public void startDocument() throws SAXException {
            
        }

        public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
            if (localName.equals("item")) {
                mConfigItem = new ConfigItem();
                mType = Integer.parseInt(atts.getValue("type"));
                mConfigItem.setType(mType);
            }
            mTagName = localName;
        }

        public void characters(char[] ch, int start, int length) throws SAXException {
            if (mTagName != null) {
                String data = new String(ch,start,length);
                if (mTagName.equals("name")) {
                    mConfigItem.setName(data);
                } else if (mTagName.equals("value")) {
                    mConfigItem.setValue(data);
                }
            }

        }

        public void endElement(String uri, String localName, String name) throws SAXException {
            if (localName.equals("item")) {
                switch (mType) {
                case 0:
                    mEditor.putString(mConfigItem.getName(), mConfigItem.getValue());
                    break;
                case 1:
                    mEditor.putInt(mConfigItem.getName(), Integer.parseInt(mConfigItem.getValue()));
                    break;
                case 2:
                    if (Integer.parseInt(mConfigItem.getValue()) == 1) {
                        mEditor.putBoolean(mConfigItem.getName(), true);
                    } else {
                        mEditor.putBoolean(mConfigItem.getName(), false);
                    }
                    break;
                }
                mEditor.commit();
                Log.d("LJK", "name = "+mConfigItem.getName()+"; value = "+ mConfigItem.getValue());
                mConfigItem = null;
                mType = null;
            }
            mTagName = null;

        }

    }

    class ConfigItem {
        private String name;
        private Integer type;
        private String value;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public Integer getType() {
            return type;
        }
        public void setType(Integer type) {
            this.type = type;
        }
        public String getValue() {
            return value;
        }
        public void setValue(String value) {
            this.value = value;
        }

    }

}
